<?PHP
  class Dashboard extends Editor{
    public $id = 1;
    public $cols;
    public $usedwidgets = null;

    function getHeader(){
      return "<link href=\"/admin/style.css\" rel=\"stylesheet\" type=\"text/css\" />
              <link href=\"/admin/inettuts.css\" rel=\"stylesheet\" type=\"text/css\" />";
    }
    
    function __construct(){
      $this->cols[] = $this->loadWidgetsByColumn(1);
      $this->cols[] = $this->loadWidgetsByColumn(2);
      $this->cols[] = $this->loadWidgetsByColumn(3);  
    }
    
    function loadWidgetsByColumn($columnId){
      $columnId = $GLOBALS['db']->EscapeString($columnId);
      $rows = $GLOBALS['db']->ReadRows("SELECT path FROM {'dbprefix'}dashboards 
                                        WHERE col = '".$columnId."' AND id = '".$this->id."'
                                           ORDER BY row");
      if($rows){
        foreach($rows as $row){
          $widgetData = WidgetController::getWidgetData($row->path);
          $widget = WidgetController::getWidget($widgetData);
          $widget->load();
          $res[] = $widget;
        }
      }
      
      return $res;
    }
    
    function display(){
      echo "<div id=\"columns\">";
      $this->displayColumn(0);
      $this->displayColumn(1);
      $this->displayColumn(2);
      ?>
        <script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.2.6.min.js"></script>
        <script type="text/javascript" src="/admin/jquery-ui-personalized-1.6rc2.min.js"></script>
        <script type="text/javascript" src="/admin/inettuts.js"></script>
      <?PHP
      echo "</div>";
    }
    
    public function getEditableCode(){
      $template = new Template();
      $template->load("control_dashboardedit");
      $template->assign_var("URL",$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']);
      $widgets = WidgetController::getAllWidgets();
      foreach($widgets as $widget){
        $widget->load();
      }
      $i = 1;
      $pos = 0;
      foreach($this->cols as $col){
        if($col){
          foreach($col as $widget){
            $index = $template->add_loop_item("COLUMN".$i."WIDGETS");
            $this->addWidgetToEditable($template,"COLUMN".$i."WIDGETS",$widget,$index,$pos);
            $pos++;
          }
        }
        $i++;
      }
      $hiddenwidgets = $this->getHiddenWidgets();
      $i = 0;
      foreach($widgets as $widget){
        if(!in_array($widget->path,$this->usedwidgets)){
          $index = $template->add_loop_item("HIDDENWIDGETS");
          $this->addWidgetToEditable($template,"HIDDENWIDGETS",$widget,$index,$i);
          $i++;
        }
      }
      $i = 0;
      foreach($widgets as $widget){
        if(!in_array($widget->path,$this->usedwidgets)){
          $index = $template->add_loop_item("DONTUSEDWIDGETS");
          $this->addWidgetToEditable($template,"DONTUSEDWIDGETS",$widget,$index,$i);
          $i++;
        }
      }
      return $template->getCode();
    }

    private function addWidgetToEditable($template,$column,$widget,$index,$position){
      $template->assign_loop_var($column,$index,"WIDGETNAME",htmlentities($widget->headline));
      $template->assign_loop_var($column,$index,"WIDGETPATH",htmlentities($widget->path));
      $template->assign_loop_var($column,$index,"PLACE",$column."|".$position);
      $this->usedwidgets[] = $widget->path;
      return $template;
    }
    
    public function save($newPage,$oldPage){
    }
    
    function displayColumn($id){
      ?>
        <ul id="column<?PHP echo $id; ?>" class="column">
          <?PHP
            if($this->cols[$id]){
              foreach($this->cols[$id] as $widget){
                $widget->displayType = "dashboard";
                $widget->display();
              }
            }
          ?>
        </ul>
      <?PHP
    }

    function getHiddenWidgets(){
      $widgets = $GLOBALS['db']->ReadRows("SELECT class,{'dbprefix'}dashboards.path FROM {'dbprefix'}dashboards INNER JOIN {'dbprefix'}widgets ON {'dbprefix'}dashboards.path = {'dbprefix'}widgets.path WHERE id = '".$GLOBALS['db']->EscapeString($this->id)."' AND col = '0'");
      if($widgets){
        foreach($widgets as $widget){
          $res[] = WidgetController::getWidgetData($widget);
        }
      }
      return $res;
    }
  }
?>